package me.zhengjie.hr.employeestate.rest;

import me.zhengjie.aop.log.Log;
import me.zhengjie.hr.employeestate.domain.EmployeeState;
import me.zhengjie.hr.employeestate.service.EmployeeStateService;
import me.zhengjie.hr.employeestate.service.dto.EmployeeStateQueryCriteria;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @author ZGZ
* @date 2020-04-15
*/
@Api(tags = "人员动态管理")
@RestController
@RequestMapping("/api/employeeState")
public class EmployeeStateController {

    private final EmployeeStateService employeeStateService;

    public EmployeeStateController(EmployeeStateService employeeStateService) {
        this.employeeStateService = employeeStateService;
    }

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('employeeState:list')")
    public void download(HttpServletResponse response, EmployeeStateQueryCriteria criteria) throws IOException {
        employeeStateService.download(employeeStateService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询人员动态")
    @ApiOperation("查询人员动态")
    @PreAuthorize("@el.check('employeeState:list')")
    public ResponseEntity<Object> getEmployeeStates(EmployeeStateQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(employeeStateService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增人员动态")
    @ApiOperation("新增人员动态")
    @PreAuthorize("@el.check('employeeState:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody EmployeeState resources){
        return new ResponseEntity<>(employeeStateService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改人员动态")
    @ApiOperation("修改人员动态")
    @PreAuthorize("@el.check('employeeState:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody EmployeeState resources){
        employeeStateService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除人员动态")
    @ApiOperation("删除人员动态")
    @PreAuthorize("@el.check('employeeState:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody Integer[] ids) {
        employeeStateService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}